package org.sun.sample.pojo;

import sun.net.www.protocol.http.HttpURLConnection;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.concurrent.*;

/**
 * Created by M on 16/4/20.
 */
public class CallableSample {

    public CallableSample() {
        ExecutorService service = Executors.newCachedThreadPool();

        Future<String> future = service.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                return CallableSample.loadDataFromNet();
            }
        });
        service.shutdownNow();
        try {
            System.out.println(future.get());
        } catch (Exception exp){

        }
    }

    public static final String loadDataFromNet() {
        try {
            URL url = new URL("http://www.baidu.com");
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            conn.getContent();
            BufferedInputStream inputStream = new BufferedInputStream(conn.getInputStream());
            byte[] data = new byte[1024];
            String message = "";
            while (inputStream.read(data) > 0) {
                message = message.concat(new String(data));
            }
            return message;

        } catch (Exception exp) {
            exp.printStackTrace();
        }
        return "nil";
    }

}
